﻿using VNStories.Data;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using System.Collections.ObjectModel;
using VNStories.VnStoriesService;
using Windows.UI.Popups;

// The Grouped Items Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234231

namespace VNStories
{
    /// <summary>
    /// A page that displays a grouped collection of items.
    /// </summary>
    public sealed partial class GroupedItemsPage : VNStories.Common.LayoutAwarePage
    {
        public GroupedItemsPage()
        {
            this.InitializeComponent();

            this.Loaded += GroupedItemsPage_Loaded;

            
        }


        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            prbLoading.Visibility = Visibility.Collapsed;
            if (App.DataSource.AllGroups == null)
            {
                btnReloadInfo.Visibility = Visibility.Visible;
            }
            else if (App.DataSource.AllGroups.Count <= 0)
            {
                btnReloadInfo.Visibility = Visibility.Visible;
            }
            else btnReloadInfo.Visibility = Visibility.Collapsed;
            base.OnNavigatedTo(e);
        }

        void data_OnLoadingError(string message)
        {
            MessageDialog dx = new MessageDialog("Application could not start normally because some errors happened. Please check your network connection to make sure that VietnamStories can get data." +
                "\n If you cant resolve it, feel free to contact us. \n Thanks", "Oops, something wrongs");
            dx.ShowAsync();
            prbLoading.Visibility = Visibility.Collapsed;
            btnReloadInfo.Visibility = Visibility.Visible;
        }

        void GroupedItemsPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (App.DataSource != null)
            {
                if (App.DataSource.AllGroups.Count > 0)
                {
                    //k load
                }
                else
                {
                    LoadData();
                    
                }
            }
            else
            {

                LoadData();
 
            }


        }

        void data_OnDataLoaded(StoriesDataSource data)
        {
            App.DataSource = data;
            this.DefaultViewModel["Groups"] = App.DataSource.AllGroups;
            prbLoading.Visibility = Visibility.Collapsed;
            btnReloadInfo.Visibility = Visibility.Collapsed;
            
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            this.DefaultViewModel["Groups"] = App.DataSource.AllGroups;
        }

        /// <summary>
        /// Invoked when a group header is clicked.
        /// </summary>
        /// <param name="sender">The Button used as a group header for the selected group.</param>
        /// <param name="e">Event data that describes how the click was initiated.</param>
        void Header_Click(object sender, RoutedEventArgs e)
        {
            // Determine what group the Button instance represents
            var group = (sender as FrameworkElement).DataContext;

            // Navigate to the appropriate destination page, configuring the new page
            // by passing required information as a navigation parameter
            this.Frame.Navigate(typeof(GroupDetailPage), ((SampleDataGroup)group).UniqueId);
        }

        /// <summary>
        /// Invoked when an item within a group is clicked.
        /// </summary>
        /// <param name="sender">The GridView (or ListView when the application is snapped)
        /// displaying the item clicked.</param>
        /// <param name="e">Event data that describes the item clicked.</param>
        void ItemView_ItemClick(object sender, ItemClickEventArgs e)
        {
            // Navigate to the appropriate destination page, configuring the new page
            // by passing required information as a navigation parameter
            var itemId = ((SampleDataItem)e.ClickedItem).UniqueId;
            this.Frame.Navigate(typeof(ItemDetailPage), itemId);
        }

        private void Refresh_Click_1(object sender, RoutedEventArgs e)
        {
            LoadData();
            
        }

        private void Help_Click_1(object sender, RoutedEventArgs e)
        {
            MessageDialog dlg = new MessageDialog("Program will load data from internet");
            dlg.ShowAsync();
        }

        private void btnReloadInfo_Click_1(object sender, RoutedEventArgs e)
        {
            //load
            LoadData();
            
        }

        private void LoadData()
        {
            DataLoading data = new DataLoading();
            btnReloadInfo.Visibility = Visibility.Collapsed;
            prbLoading.IsIndeterminate = true;
            prbLoading.Visibility = Visibility.Visible;
            data.OnDataLoaded += data_OnDataLoaded;
            data.OnLoadingError += data_OnLoadingError;
            data.Load();

        }

        private void btnReloadInfo_Tapped(object sender, TappedRoutedEventArgs e)
        {
            LoadData();
        }
    }
}
